﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Instances.Contracts;
using Umbrella.Net.Contracts;

namespace Umbrella.Whs.Service.Activities
{

    public sealed class GetNewerVersionInfo : SafeCodeActivity
    {
        public OutArgument<UmbrellaReleaseInfo> LatesVersionInfo { get; set; }
        public OutArgument<int> CurrentVersionId { get; set; }
        public OutArgument<bool> IsNewer { get; set; }

        protected override void ExecuteSafe(CodeActivityContext context)
        {
            using (var wsc = new UmbrellaServiceClientFactory())
            {
                var q = new LatestVersionQuery()
                {
                    ProductId = UmbrellaVersion.ProductId,
                    CurrentVersionId = UmbrellaVersion.VersionId,
                    InstanceId = UmbrellaRegistry.GetInstanceId(),
                };

                var nv = wsc.CreateChannel().GetLatestVersion(q);

                // finalize
                context.SetValue(this.LatesVersionInfo, nv);
                context.SetValue(this.CurrentVersionId, UmbrellaVersion.VersionId);
                context.SetValue(this.IsNewer, (nv.ReleaseId > UmbrellaVersion.VersionId));
            }
        }
    }
}
